#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
#include <climits>

using namespace std;

void delspace(string &str)
{
    while (str.back() == ' ')
    {
        str.pop_back();
    }
    for (int i = 0; i < str.size(); i++)
    {
        if (str[i] != ' ')
        {
            str = str.substr(i, str.size() - (i));
            return;
        }
    }
}
int main()
{
    //freopen("1", "r", stdin);
    int n;
    map<string, string> config;
    map<string, int> configFlag;
    cin >> n;
    string nothing;
    getline(cin, nothing);
    for (int i = 0; i < n; i++)
    {
        string str;
        getline(cin, str);
        delspace(str);
        if (str.size() == 0 || str[0] == '#' || count(str.begin(), str.end(), '=') == 0)
            continue;
        auto it = find(str.begin(), str.end(), '=') - str.begin();
        string key = str.substr(0, it);
        string value = str.substr(it + 1, str.size() - (it + 1));
        delspace(value);
        delspace(key);
        config[key] = value;
        if (key.size() == 0 || value.size() == 0)
        {
            cout << "ERROR" << endl;
            return 0;
        }
        //cout << key << " " << value << endl;
    }
    for (auto &[key, value] : config)
    {
        int cnt = 0;
        for (char c : value)
        {
            if (c == '{')
            {
                configFlag[key] = 1;
                cnt++;
            }
            else if (c == '}')
                cnt--;
            if (cnt > 1 || cnt < 0)
            {
                cout << "ERROR" << endl;
                return 0;
            }
        }
        if (cnt != 0)
        {
            cout << "ERROR" << endl;
            return 0;
        }
    }
    map<string, set<string>> sc;
    map<string, string> trueConfig;
    int T = 10;
    while (T--)
    {
        trueConfig.clear();
        for (auto &[key, value] : config)
        {
            if (configFlag[key] == 1)
            {
                string newValue = "";
                for (int i = 0; i < value.size(); i++)
                {

                    if (value[i] == '{')
                    {
                        string P = "";
                        i++;

                        while (i < value.size() && value[i] != '}')
                        {

                            P += value[i];
                            i++;
                        }

                        if (!config.count(P))
                        {
                            cout << "ERROR" << endl;
                            return 0;
                        }
                        sc[key].insert(P);
                        if (sc[P].count(key))
                        {
                            cout << "ERROR" << endl;
                            return 0;
                        }
                        newValue += config[P];
                    }
                    else
                    {
                        newValue += value[i];
                    }
                }
                trueConfig[key] = newValue;
            }
            else
                trueConfig[key] = value;
        }
        config = trueConfig;
    }

    string Key;
    cin >> Key;
    delspace(Key);
    if (!trueConfig.count(Key))
    {
        cout << "NULL" << endl;
    }
    else
    {
        cout << trueConfig[Key] << endl;
    }
}